﻿/*+===================================================================
File:      GameOverState.cpp

Summary:   Hiện thực hóa các phương thức của lớp CGameOverState
===================================================================+*/

#include "GameOverState.h"

CGameOverState::CGameOverState()
{
	m_bTwinkle = true;
	Init();
}

CGameOverState::~CGameOverState()
{
	if (m_pScore)
		delete m_pScore;
	if (m_pChooseButton)
		delete m_pChooseButton;
	if (m_pSound)
		delete m_pSound;
}

void CGameOverState::Init()
{
	m_pScore = CResourcesManager::GetInstance()->GetTexture(ID_TEXTURE_PLAYER1_TEXTURE);
	m_pChooseButton = CResourcesManager::GetInstance()->GetTexture(ID_TEXTURE_CHOOSE_BUTTON);
	m_pSound = CResourcesManager::GetInstance()->GetSound(ID_SOUND_GAME_OVER_THEME);
	m_pSound->Play();
	m_bTwinkle = false;
	m_fTwinkleTime = 0.0f;
	m_fTimeEnd = 2.0f;
	m_bContinue = true;
}

void CGameOverState::Update(float timeFrame)
{
	if (m_bFinished)
		return;
	if (m_fTwinkleTime > 0)
		m_fTwinkleTime -= timeFrame;
	else
	{
		m_fTwinkleTime = 0.2f;
		m_bTwinkle = !m_bTwinkle;
	}
	
	if (CInput::GetInstance()->IsKeyPress(DIK_RETURN))
	{
		this->End();
	}
	

	if (CInput::GetInstance()->IsKeyPress(DIK_SPACE))
	{
		m_bContinue = !m_bContinue;
	}

	if (m_bContinue)
	{
		m_ButtonPos = D3DXVECTOR2(223, 305);
	}
	else
	{
		m_ButtonPos = D3DXVECTOR2(223, 335);
	}
}

void CGameOverState::Render()
{
	if (m_bFinished)
		return;
	LPD3DXFONT* ppFont = CGraphic::GetInstance()->GetFontHandle();
	LPD3DXSPRITE ppSprite = CGraphic::GetInstance()->GetSpriteHandler();

	char info[50];
	RECT destSource;

	sprintf_s(info, "1P    : %d ", g_nScore);
	destSource.left = 44;
	destSource.top = 64;
	destSource.bottom = 79;
	destSource.right = 300;

	(*ppFont)->DrawTextA(ppSprite, info, strlen(info), &destSource, DT_LEFT, 0xFFFFFFFF);

	int hiScore;

	switch (g_nCurentLvl)
	{
	case ID_LEVEL1:
		hiScore = MAX_SCORE_LEVEL_1;
		break;
	case ID_LEVEL2:
		hiScore = MAX_SCORE_LEVEL_2;
		break;
	case ID_LEVEL3:
		hiScore = MAX_SCORE_LEVEL_3;
		break;
	}

	sprintf_s(info, "HI : %d", hiScore);
	destSource.left = 116;
	destSource.top = 145;
	destSource.bottom = 160;
	destSource.right = 312;

	(*ppFont)->DrawTextA(ppSprite, info, strlen(info), &destSource, DT_LEFT, 0xFFFFFFFF);

	if (m_bTwinkle)
	{
		m_pScore->Draw(D3DXVECTOR2(270, 63));
		m_pScore->Draw(D3DXVECTOR2(280, 145));
	}

	sprintf_s(info, "GAME OVER");
	destSource.left = 210;
	destSource.top = 255;
	destSource.bottom = 270;
	destSource.right = 400;

	(*ppFont)->DrawTextA(ppSprite, info, strlen(info), &destSource, DT_CENTER, 0xFFFFFFFF);


	sprintf_s(info, "CONTINUE");
	destSource.left = 250;
	destSource.top = 300;
	destSource.bottom = 315;
	destSource.right = 400;

	(*ppFont)->DrawTextA(ppSprite, info, strlen(info), &destSource, DT_LEFT, 0xFFFFFFFF);

	sprintf_s(info, "END");
	destSource.left = 250;
	destSource.top = 330;
	destSource.bottom = 345;
	destSource.right = 400;

	(*ppFont)->DrawTextA(ppSprite, info, strlen(info), &destSource, DT_LEFT, 0xFFFFFFFF);

	m_pChooseButton->Draw(m_ButtonPos);
}

void CGameOverState::End()
{
	m_bFinished = true;
	delete m_pNextState;
	if (m_bContinue)
	{
		g_nScore = 0;
		g_nLives = 2;
		g_nCurrentBullet = ID_R_BULLET;
		m_pNextState = new CScoreState();
	}
	else
	{
		m_pNextState = new CEndState();
	}
	CGameStateManager::GetInstance()->ChangeState(m_pNextState);
}